import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.bson.Document;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class test2 {

    static MongoClient mongoClient = new MongoClient("192.168.1.173", 27017);
    static MongoDatabase mDatabase = mongoClient.getDatabase("企查查");
    static MongoCollection collection = mDatabase.getCollection("原表");

    public static void main(String[] args) throws IOException, InvalidFormatException {
        String file_path = "D:\\所有程序\\程存淦\\高新技术企业企查查(基本信息)\\高新技术企业企查查(基本信息)\\";
        File[] dir = new File(file_path).listFiles();
        int count = 0;
        for (File file : dir) {
            FileInputStream in = new FileInputStream(file);
            Workbook workbook = new XSSFWorkbook(in);

            Sheet sheet = workbook.getSheetAt(0);
            List<String> list = new ArrayList<>();
            count++;
            //标题 -> 字段名

            int n = sheet.getRow(0).getPhysicalNumberOfCells();
            Row r1 = sheet.getRow(0);
            for (int c = 0; c < n; c++) {
                Cell cell = r1.getCell(c);
                String cellValue = null;
                cell.setCellType(Cell.CELL_TYPE_STRING);
                cellValue = cell.getStringCellValue();
                list.add(cellValue);
            }

            //总行数
            int rowCount = sheet.getPhysicalNumberOfRows();
            //遍历每一行
            //前两行是广告

            for (int r = 2; r < rowCount; r++) {
                Row row = sheet.getRow(r);
                //int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
                Cell cell = row.getCell(0);
                String cellValue = null;

                cell.setCellType(Cell.CELL_TYPE_STRING);
                cellValue = cell.getStringCellValue();

                Document doc = new Document("_id", cellValue);

//                //遍历每一个单元格
//                for (int c = 0; c < cellCount; c++) {
//                    Cell cell = row.getCell(c);
//                    // cellType = cell.getCellType();
//                    String cellValue = null;
//
//                    //在读取单元格内容前,设置所有单元格中内容都是字符串类型
//                    cell.setCellType(Cell.CELL_TYPE_STRING);
//
//                    //按照字符串类型读取单元格内数据
//                    cellValue = cell.getStringCellValue();
//                    doc.append(list.get(c), cellValue);
//                }


                try {
                    BasicDBObject newObj = new BasicDBObject("$set", doc);
                    BasicDBObject query = new BasicDBObject().append("_id", doc.get("_id"));
                    collection.updateOne(query, newObj, (new UpdateOptions()).upsert(true));
                    System.out.println("公司名称   " + doc.get("_id"));
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
            System.out.println(count);

        }
    }
}
